package com.ooono.factorytest.items;

import android.app.Service;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;

import com.ooono.factorytest.R;
import com.ooono.factorytest.base.BaseActivity;

import java.io.IOException;

public class SpeakerTestActivity extends BaseActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

    private static final String TAG = "SpeakerTestActivity";

    public static final String KEY = "speaker";

    private MediaPlayer mMediaPlayer;
    private AudioManager mAudioManager;
    private int currentMediaVolume;
    private int currentCallVolume;
    private int currentSystemVolume;
    private int currentAlarmVolume;

    @Override
    public int getContentLayout() {
        return R.layout.content_speaker_test;
    }

    @Override
    public void initViews() {

    }

    @Override
    public int getTestTitle() {
        return R.string.speaker_test_title;
    }

    @Override
    public String getTestKey() {
        return KEY;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
        currentMediaVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        currentCallVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
        currentSystemVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
        currentAlarmVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        Log.d(TAG, "current volume media: " + currentMediaVolume + ", call: " + currentCallVolume + ", system: " + currentSystemVolume +
                ", alarm: " + currentAlarmVolume);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) (currentMediaVolume * 0.6), AudioManager.FLAG_PLAY_SOUND);
        play(R.raw.play);
    }

    @Override
    protected void onStop() {
        stop();
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentMediaVolume, AudioManager.FLAG_PLAY_SOUND);
        mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, currentCallVolume, AudioManager.FLAG_PLAY_SOUND);
        mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, currentSystemVolume, AudioManager.FLAG_PLAY_SOUND);
        mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, currentAlarmVolume, AudioManager.FLAG_PLAY_SOUND);
        super.onStop();
    }

    public void play(int resId) {
        stop();
        AssetFileDescriptor file = getResources().openRawResourceFd(resId);

        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaPlayer.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mMediaPlayer.start();
    }
}
